package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.KfnWorkOrderVo;
import com.ruoyi.system.domain.bo.KfnWorkOrderBo;
import com.ruoyi.system.service.IKfnWorkOrderService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单池
 *
 * @author Panda
 * @date 2023-12-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/workOrder")
public class KfnWorkOrderController extends BaseController {

    private final IKfnWorkOrderService iKfnWorkOrderService;

    /**
     * 查询工单池列表
     */
    @SaCheckPermission("system:workOrder:list")
    @GetMapping("/list")
    public TableDataInfo<KfnWorkOrderVo> list(KfnWorkOrderBo bo, PageQuery pageQuery) {
        return iKfnWorkOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工单池列表
     */
    @SaCheckPermission("system:workOrder:export")
    @Log(title = "工单池", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KfnWorkOrderBo bo, HttpServletResponse response) {
        List<KfnWorkOrderVo> list = iKfnWorkOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "工单池", KfnWorkOrderVo.class, response);
    }

    /**
     * 获取工单池详细信息
     *
     * @param workOrderId 主键
     */
    @SaCheckPermission("system:workOrder:query")
    @GetMapping("/{workOrderId}")
    public R<KfnWorkOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String workOrderId) {
        return R.ok(iKfnWorkOrderService.queryById(workOrderId));
    }

    /**
     * 新增工单池
     */
    @SaCheckPermission("system:workOrder:add")
    @Log(title = "工单池", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KfnWorkOrderBo bo) {
        return toAjax(iKfnWorkOrderService.insertByBo(bo));
    }

    /**
     * 修改工单池
     */
    @SaCheckPermission("system:workOrder:edit")
    @Log(title = "工单池", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KfnWorkOrderBo bo) {
        return toAjax(iKfnWorkOrderService.updateByBo(bo));
    }

    /**
     * 删除工单池
     *
     * @param workOrderIds 主键串
     */
    @SaCheckPermission("system:workOrder:remove")
    @Log(title = "工单池", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workOrderIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] workOrderIds) {
        return toAjax(iKfnWorkOrderService.deleteWithValidByIds(Arrays.asList(workOrderIds), true));
    }
}
